﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using gd.Biz.ExcelImport;

namespace ExcelModuleWeb.Controllers
{
    public class ExcelImportController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configId"></param>
        /// <returns></returns>
        public ActionResult Index(string configId)
        {
            // 1.根据配置读取相应的视图
            // 2.检测当前用户是否有操作当前导入的权限
            // 3.返回相应视图

            //var userId = this.SessionUser.Id;
            var userId = 1;

            var biz = new ExcelImportBiz();
            if (biz.CheckRole(configId, userId) == true)
            {
                var config = biz.LoadConfig(configId, userId);
                return View(config.View, config.ViewData);
            }
            else
            {
                throw new Exception("没有权限");
            }
        }

        /// <summary>
        /// 导入前检测
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ActionResult ImportCheck(string configId)
        {
            //var userId = this.SessionUser.Id;
            var userId = 1;
            object obj = null;
            var biz = new ExcelImportBiz();
            var model = biz.ImportCheck(configId, userId, obj, Request["fileName"]);
            return Json(model);
        }

        /// <summary>
        /// 检测后导入
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ActionResult ImportStart(string configId)
        {
            //var userId = this.SessionUser.Id;
            var userId = 1;
            object obj = null;
            var biz = new ExcelImportBiz();
            var model = biz.ImportStart(configId, userId, obj, Request["fileName"]);
            return Json(model);
        }

        /// <summary>
        /// 下载模板
        /// </summary>
        /// <param name="configId"></param>
        /// <returns></returns>
        public void DownloadTemplate(string configId)
        {
            var biz = new ExcelImportBiz();
            biz.DownloadTemplateFile(configId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="resultId"></param>
        public void DownloadResult(string resultId)
        {
            var biz = new ExcelImportBiz();
            biz.DownloadResultFile(resultId);
        }
    }
}